package com.lib.torment.androidlib.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;

import com.lib.torment.androidlib.R;
import com.lib.torment.androidlib.core.ActivityManager;
import com.lib.torment.androidlib.widget.DividerItemDecoration;

import static com.lib.torment.androidlib.core.AppConfigs.REFRESH_STATE_INIT;
import static com.lib.torment.androidlib.core.AppConfigs.REFRESH_STATE_REFRESH;

/**
 * Created by Torment on 2016/9/13.
 */
public abstract class AppActivity extends AppCompatActivity {
    public static final int[] SWIPE_REFRESH_COLOR = {R.color.color_swipe_blue,
            R.color.color_swipe_red, R.color.color_swipe_yellow, R.color.color_swipe_green};


    protected RecyclerView mRecyclerView;
    protected LinearLayoutManager mLinearLayoutManager;
    protected SwipeRefreshLayout mSwipeRefreshLayout;

    protected Toolbar mToolbar;

    /**
     * 记录当前Activity的加载状态，默认为初始化。<br/>
     * 通过这个标识可以判断是否加载网络失败的视图
     */
    protected int mRefreshState = REFRESH_STATE_INIT;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActivityManager.getInstance().addActivity(this);

        int res = setContentView();
        if (res > 0) {
            setContentView(res);
        }
        initVariables();
        initViews();
        initDatas();
        initViewListener();
    }

    /**
     * 初始化布局
     *
     * @return
     */
    protected abstract int setContentView();

    /**
     * 初始化变量<br/>
     * 例如Activity携带的数据
     */
    protected abstract void initVariables();

    /**
     * 初始化布局
     */
    protected abstract void initViews();

    /**
     * 初始化布局数据
     */
    protected abstract void initDatas();


    /**
     * 初始化布局监听
     */
    protected abstract void initViewListener();

    /**
     * 获得通用的Toolbar
     *
     * @param toolbarResId Toolbar资源ID
     * @param menuResId    menu资源ID
     * @return Toolbar 实例
     */
    protected Toolbar getToolbar(int toolbarResId, int menuResId, boolean isDisplayHomeAsUpEnabled) {
        if (mToolbar == null) {
            mToolbar = (Toolbar) findViewById(toolbarResId);
            if (mToolbar != null) {
                mToolbar.setTitle("");
                if (isDisplayHomeAsUpEnabled) {
                    mToolbar.setNavigationIcon(R.drawable.ic_navigation_arrow_back);
                    mToolbar.setNavigationOnClickListener(v -> finish());
                }
                if (menuResId > 0) {
                    mToolbar.inflateMenu(menuResId);
                    mToolbar.setOnMenuItemClickListener(AppActivity.this::onMenuItemClick);
                }
            }
        }
        return mToolbar;
    }


    protected boolean onMenuItemClick(MenuItem item) {
        return false;
    }

    /**
     * 获得一个线性的 RecyclerView
     *
     * @param viewResId        RecyclerView 资源ID
     * @param orientation      水平还是垂直
     * @param isItemDecoration 是否添加分割线
     * @return RecyclerView
     */
    protected RecyclerView getLinearLayoutRecyclerView(int viewResId, int orientation, boolean isItemDecoration) {
        if (mRecyclerView == null) {
            mRecyclerView = (RecyclerView) findViewById(viewResId);
            if (mRecyclerView != null) {
                mLinearLayoutManager = new LinearLayoutManager(this);
                mLinearLayoutManager.setOrientation(orientation);
                mRecyclerView.setLayoutManager(mLinearLayoutManager);

                if (isItemDecoration) {
                    mRecyclerView.addItemDecoration(new DividerItemDecoration(this, LinearLayoutManager.VERTICAL));
                }
            } else {
                throw new RuntimeException("AppActivity: RecyclerView is null");
            }
        }
        return mRecyclerView;
    }

    /**
     * 获得初始化成功的SwipeRefreshLayout，并可以控制是否在初始化时刷新
     *
     * @param viewResId     SwipeRefreshLayout资源ID
     * @param isRefresh     在初始化时是否刷新
     * @param isShowRefresh 在初始化时是否显示刷新的圆圈
     * @return SwipeRefreshLayout
     */
    protected SwipeRefreshLayout getSwipeRefreshLayout(int viewResId, boolean isRefresh, final boolean isShowRefresh) {
        if (mSwipeRefreshLayout == null) {
            mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(viewResId);
            if (mSwipeRefreshLayout != null) {
                mSwipeRefreshLayout.setColorSchemeResources(SWIPE_REFRESH_COLOR);
            } else {
                throw new RuntimeException("AppActivity: SwipeRefreshLayout is null");
            }

            if (isRefresh) {
                mSwipeRefreshLayout.post(new Runnable() {
                    @Override
                    public void run() {
                        if (isShowRefresh) {
                            openSwipeRefreshLayoutRefresh();
                        }
                        AppActivity.this.onRefresh(REFRESH_STATE_INIT);
                        mRefreshState = REFRESH_STATE_INIT;
                    }
                });
            }

            mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
                @Override
                public void onRefresh() {
                    AppActivity.this.onRefresh(REFRESH_STATE_REFRESH);
                    mRefreshState = REFRESH_STATE_REFRESH;
                }
            });
        }
        return mSwipeRefreshLayout;
    }

    protected SwipeRefreshLayout openSwipeRefreshLayoutRefresh() {
        return setSwipeRefreshLayoutRefreshState(true);
    }

    protected SwipeRefreshLayout closeSwipeRefreshLayoutRefresh() {
        return setSwipeRefreshLayoutRefreshState(false);
    }

    protected SwipeRefreshLayout setSwipeRefreshLayoutRefreshState(boolean isRefresh) {
        if (mSwipeRefreshLayout != null) {
            mSwipeRefreshLayout.setRefreshing(isRefresh);
        }
        return mSwipeRefreshLayout;
    }

    /**
     * 如果当前布局需要刷新功能的话，可以重写此方法
     *
     * @param refreshState
     */
    protected void onRefresh(int refreshState) {
    }

    protected void startIntent(Class<?> cla) {
        startIntent(cla, false);
    }

    protected void startIntent(Class<?> cla, boolean finishState) {
        Intent intent = new Intent(this, cla);
        startActivity(intent);
        if (finishState) {
            finish();
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int itemId = item.getItemId();
        switch (itemId) {
            case android.R.id.home:
                finish();
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ActivityManager.getInstance().removeActivity(this);
    }
}
